package com.cubeofnine.sugar.business;

import java.net.URL;
import java.util.Vector;

import com.cubeofnine.sugar.service.SugarCrmService;
import com.cubeofnine.sugar.service.element.DatabaseElement;
import com.cubeofnine.sugar.service.element.EntryListResult;
import com.cubeofnine.sugar.service.element.EntryResult;
import com.cubeofnine.sugar.service.element.ErrorValue;
import com.cubeofnine.sugar.service.element.ModuleFields;
import com.cubeofnine.sugar.service.element.RelationshipsResult;

import android.content.Context;
import android.util.Log;

public class BusinessImpt {
	public static final String MODULE_CONTACTS = "Contacts";
	public static final String MODULE_ACCOUNTS = "Accounts";

	private SugarCrmService serviceAdapter = null;
	private String userName = null;
	private String password = null;
	private String sessionId = null;
	private String moduleName = null;
	private String url = null;

	public static String getClassName() {
		return BusinessImpt.class.getName();
	}

	public BusinessImpt(Context context, String moduleName) throws Exception {
		LoginBusiness loginBusiness = new LoginBusiness(context);
		DatabaseElement databaseElement = loginBusiness.getDefaultValue();

		String sugarUrl = databaseElement.getSugarUrl();
		String sessionId = databaseElement.getSessionId();
		String userName = databaseElement.getUsername();
		String password = databaseElement.getPassword();

		Log.d(getClassName(), "context = " + context);
		Log.d(getClassName(), "userName = " + userName);
		Log.d(getClassName(), "password = " + password);
		Log.d(getClassName(), "sessionId = " + sessionId);
		Log.d(getClassName(), "moduleName = " + moduleName);
		Log.d(getClassName(), "sugarUrl = " + sugarUrl);
		try {
			this.userName = userName;
			this.password = password;
			this.sessionId = sessionId;
			this.moduleName = moduleName;
			this.url = sugarUrl;
			this.serviceAdapter = new SugarCrmService(sugarUrl);
		} catch (Exception e) {
			throw e;
		}
	}

	public ModuleFields getModuleFields() throws Exception {
		Log.i(getClassName(), "getModuleFields() : Start");
		ModuleFields result = null;

		try {
			result = serviceAdapter.getModuleFields(sessionId, moduleName);
		} catch (Exception e) {
			throw e;
		}

		Log.i(getClassName(), "getModuleFields() : End");
		return result;
	}

	public EntryListResult getEntryList(String query, String orderBy,
			int offset, Vector<String> fieldName, int maxResults, int deleted)
			throws Exception {
		return getEntryList(moduleName, query, orderBy, offset, fieldName,
				maxResults, deleted);
	}

	public EntryListResult getEntryList(String moduleName, String query,
			String orderBy, int offset, Vector<String> fieldName,
			int maxResults, int deleted) throws Exception {
		Log.i(getClassName(), "getEntryList() : Start");
		EntryListResult result = null;

		try {
			result = serviceAdapter.getEntryList(sessionId, moduleName, query,
					orderBy, offset, fieldName, maxResults, deleted);
		} catch (Exception e) {
			throw e;
		}

		Log.i(getClassName(), "getEntryList() : End");
		return result;
	}

	public EntryResult getEntry(String id, Vector<String> fieldName)
			throws Exception {
		Log.i(getClassName(), "getEntryList() : Start");
		EntryResult result = null;

		try {
			result = serviceAdapter.getEntry(sessionId, moduleName, id,
					fieldName);
		} catch (Exception e) {
			throw e;
		}

		Log.i(getClassName(), "getEntryList() : End");
		return result;
	}

	public EntryResult getEntry(String moduleName, String id,
			Vector<String> fieldName) throws Exception {
		Log.i(getClassName(), "getEntryList() : Start");
		EntryResult result = null;

		try {
			result = serviceAdapter.getEntry(sessionId, moduleName, id,
					fieldName);
		} catch (Exception e) {
			throw e;
		}

		Log.i(getClassName(), "getEntryList() : End");
		return result;
	}

	public RelationshipsResult getRelationships(String moduleName,
			String moduleId, String relatedModule, String relatedModuleQuery,
			boolean isDeleted) throws Exception {
		Log.i(getClassName(), "getRelationships() : Start");
		RelationshipsResult result = null;

		try {
			result = serviceAdapter.getRelationships(sessionId, moduleName,
					moduleId, relatedModule, relatedModuleQuery, isDeleted);
		} catch (Exception e) {
			throw e;
		}

		Log.i(getClassName(), "getRelationships() : End");
		return result;
	}

	public RelationshipsResult getAccountsContacts(String contactId)
			throws Exception {
		return getRelationships(MODULE_CONTACTS, contactId, MODULE_ACCOUNTS,
				"", false);
	}

	public RelationshipsResult getAccountsContacts(String contactId,
			String query, boolean isDeleted) throws Exception {
		return getRelationships(MODULE_CONTACTS, contactId, MODULE_ACCOUNTS,
				query, isDeleted);
	}

	public ErrorValue setRelationship(String moduleName, String moduleId,
			String relateModule, String relateId) throws Exception {
		return serviceAdapter.setRelationship(sessionId, moduleName, moduleId,
				relateModule, relateId);
	}

	public String createContact(String firstName, String lastName, String email)
			throws Exception {
		return serviceAdapter.createContact(userName, password, firstName,
				lastName, email);
	}

	public String createAccount(String name, String phone, String website)
			throws Exception {
		return serviceAdapter.createAccount(userName, password, name, phone,
				website);
	}

	public SugarCrmService getServiceAdapter() {
		return serviceAdapter;
	}

	public void setServiceAdapter(SugarCrmService serviceAdapter) {
		this.serviceAdapter = serviceAdapter;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public String getModuleName() {
		return moduleName;
	}

	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}

	public String getUrlString() {
		return url;
	}

	public URL getUrl() throws Exception {
		return new URL(url);
	}

	public String getUrlRootAddress() throws Exception {
		String protocol = getUrl().getProtocol();
		String host = getUrl().getHost();
		int port = getUrl().getPort();
		return protocol + "://" + host + (port == -1 ? "" : ":" + port) + "/";
	}

	public void setUrl(String url) {
		this.url = url;
	}

}
